package gdufs.lkl.calculator;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 简单的设置管理器
 * 只管理小数位数和历史记录条数设置
 */
public class SettingsManager {
    private static final String PREFS_NAME = "simple_calculator_settings";
    
    // 设置键名
    private static final String KEY_DECIMAL_PLACES = "decimal_places";
    private static final String KEY_HISTORY_MAX_COUNT = "history_max_count";
    
    // 默认值
    public static final int DEFAULT_DECIMAL_PLACES = 10;
    public static final int DEFAULT_HISTORY_MAX_COUNT = 100;
    
    private static SettingsManager instance;
    private SharedPreferences prefs;
    
    private SettingsManager(Context context) {
        prefs = context.getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
    
    public static synchronized SettingsManager getInstance(Context context) {
        if (instance == null) {
            instance = new SettingsManager(context);
        }
        return instance;
    }
    
    // 小数位数设置
    public int getDecimalPlaces() {
        return prefs.getInt(KEY_DECIMAL_PLACES, DEFAULT_DECIMAL_PLACES);
    }
    
    public void setDecimalPlaces(int decimalPlaces) {
        prefs.edit().putInt(KEY_DECIMAL_PLACES, decimalPlaces).apply();
    }
    
    // 历史记录最大条数设置
    public int getHistoryMaxCount() {
        return prefs.getInt(KEY_HISTORY_MAX_COUNT, DEFAULT_HISTORY_MAX_COUNT);
    }
    
    public void setHistoryMaxCount(int maxCount) {
        prefs.edit().putInt(KEY_HISTORY_MAX_COUNT, maxCount).apply();
    }
    
    // 恢复默认设置
    public void restoreDefaults() {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(KEY_DECIMAL_PLACES, DEFAULT_DECIMAL_PLACES);
        editor.putInt(KEY_HISTORY_MAX_COUNT, DEFAULT_HISTORY_MAX_COUNT);
        editor.apply();
    }
}
